package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuImage;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SkuSaleAttrValueService;
import com.atguigu.gmall.product.vo.CategoryPath;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author lfy
 * @Description
 * @create 2023-07-15 14:08
 */
@RequestMapping("/api/inner/product")
@RestController
public class SkuDetailRpcController {


    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SkuImageService skuImageService;

    @Autowired
    BaseCategory1Service baseCategory1Service;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;
    /**
     * 获取SkuInfo
     * @param skuId
     * @return
     */
    @GetMapping("/skuinfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId){
//        int i = 10/0;
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return Result.ok(skuInfo);
    }


    /**
     * 获取sku图片
     * @param skuId
     * @return
     */
    @GetMapping("/skuimages/{skuId}")
    public Result<List<SkuImage>> getSkuImages(@PathVariable("skuId") Long skuId){

        List<SkuImage> skuImages = skuImageService.lambdaQuery()
                .eq(SkuImage::getSkuId, skuId)
                .list();
        return Result.ok(skuImages);
    }

    /**
     * 获取商品的实时价格
     * @param skuId
     * @return
     */
    @GetMapping("/price/{skuId}")
    public Result<BigDecimal> getSkuPrice(@PathVariable("skuId") Long skuId){
        //select price from sku_info where id=?

        BigDecimal price = skuInfoService.getRealTimePrice(skuId);
        return Result.ok(price);
    }

    /**
     * 获取商品所属的分类层级
     */
    @GetMapping("/categorypath/{c3Id}")
    public Result<CategoryPath> getCategoryPath(@PathVariable("c3Id") Long c3Id){

       CategoryPath categoryPath = baseCategory1Service.getCategoryPath(c3Id);

        return Result.ok(categoryPath);
    }

    /**
     * 查询当前sku对应的spu定义的所有销售属性名和值并标注出当前sku是哪一种组合
     * @return
     */
    @GetMapping("/saleattr/{skuId}/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrAndValueAnnotationSku(@PathVariable("skuId") Long skuId,
                                                                         @PathVariable("spuId") Long spuId){



        List<SpuSaleAttr> spuSaleAttr = skuSaleAttrValueService
                .getSpuSaleAttrAndValueAnnotationSku(skuId,spuId);
        return Result.ok(spuSaleAttr);
    }


    /**
     * 查询某个spu下的所有sku所有的销售属性组合可能性
     * @param spuId
     * @return
     */
    @GetMapping("/valueskujson/{spuId}")
    public Result<String> getValueSkuJson(@PathVariable("spuId") Long spuId){

        String json = skuSaleAttrValueService.getValueSkuJson(spuId);

        return Result.ok(json);
    }
}
